package com.steven;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.Objects;

/**
 * 带背景图的jpanel
 * qxh
 * 2019年9月19日16:42:20
 *
 * @author Administrator
 */
public class BackGroundImagePanle extends JPanel {
    private ImageIcon icon;

    /*public ImageIcon getIcon() {
        return icon;
    }*/
    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    private int x = 0;
    private int y = 0;

    public BackGroundImagePanle() {
        // TODO Auto-generated constructor stub
//        icon = new ImageIcon(this.getClass().getResource(url));
//        icon = new ImageIcon(Objects.requireNonNull(PicView.class.getResource("/bg.jpg")));
        String path = "./bg.jpg";
        File file = new File(path);
        if (!file.exists()){
            icon = new ImageIcon(Objects.requireNonNull(PicView.class.getResource("/bg.jpg")));
        }else{
            icon = new ImageIcon(Objects.requireNonNull(path));
        }

//        icon = new ImageIcon(Objects.requireNonNull(BackGroundImagePanle.class.getResource(url)));
    }

    public void paintComponent(Graphics g) {
        g.drawImage(icon.getImage(), x, y, getSize().width, getSize().height, PicView.jFrame);// 图片会自动缩放 this
    }
}